public class Solution {
    // 二分查找
    public int search(int[] nums, int target) {
        int len = nums.length;
        int left = 0, right = len - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if(nums[mid] < target) {
                left = mid + 1;
            } else if (nums[mid] > target) {
                right = mid - 1;
            } else {
                return mid;
            }
        }
        return -1;
    }

    // 在排序数组中查找元素的第一个和最后一个位置
    public int[] searchRange(int[] nums, int target) {
        int[] ret = {-1, -1};
        int len = nums.length;
        if (len == 0) {
            return ret;
        }
        int left = 0, right = len - 1;
        // 查找左端点
        while(left < right) {
            int mid = (left + right) / 2;

            if(target > nums[mid]) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        if(nums[left] == target) {
            ret[0] = left;
        }
        // 查找右端点
        left = 0;
        right = len - 1;
        while (left < right) {
            int mid = (left + right + 1) / 2;
            if (target < nums[mid]) {
                right = mid - 1;
            } else {
                left = mid;
            }
        }
        if (nums[left] == target) {
            ret[1] = left;
        }
        return ret;
    }

    // 搜索插入位置
    public int searchInsert(int[] nums, int target) {
        int len = nums.length;
        int left = 0, right = len - 1;
        if (target > nums[right]) {
            return len;
        }
        while (left < right) {
            int mid = (left + right) / 2;
            if (target > nums[mid]) {
                left = mid + 1;
            } else {
                right = mid;
            }
            System.out.println("left :" + left + " right:" + right + " mid:" + mid);
        }
        return left;
    }

    // x 的平方根
    public int mySqrt(int x) {
        long left = 0, right = x;
        while (left < right) {
            long mid = (right + left + 1) / 2;
            if ((long)(mid * mid) > x) {
                right = mid - 1;
            } else {
                left = mid;
            }
        }
        return (int)left;
    }

    // 山脉数组的峰顶索引
    public int peakIndexInMountainArray(int[] arr) {
        int len = arr.length;
        int left = 0, right = len - 1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (arr[mid] < arr[mid + 1]) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        return left;
    }

    // 寻找峰值
    public int findPeakElement(int[] nums) {
        int len = nums.length;
        int left = 0, right = len - 1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] < nums[mid + 1]) {
                left = mid + 1;
            } else {
                right = mid;
            }
            //System.out.println("left: " + left + " right: " + right + " mid:" + mid);
        }
        return left;
    }

    // 寻找旋转排序数组中的最小值
    public int findMin(int[] nums) {
        int len = nums.length;
        if (len == 1) {
            return nums[0];
        }
        int left = 0, right = len - 1;
        int x = nums[right];
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] <= x) {
                right = mid;
            } else {
                left = mid + 1;
            }
            // System.out.println("left: " + left + " right: " + right + " mid: " + mid);
        }
        return nums[left];
    }

    // 点名
    public int takeAttendance(int[] records) {
        int len = records.length;
        int left = 0, right = len - 1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (records[mid] == mid) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        return records[left] == left ? left + 1 : left;
    }
}
